package gist

import akka.actor.ActorSystem
import akka.actor.Actor
import akka.actor.Props
import java.io.FileOutputStream
import akka.event.slf4j.Logger
import akka.event.Logging

object AkkaTest extends App {

  val system = ActorSystem("test-akka")
  val myAc = system.actorOf(Props(classOf[MyActor]), "myActor")

  myAc ! "hello"

}

class MyActor extends Actor {
 val log = Logging.getLogger(context.system, this)
  import java.io.FileOutputStream
  import java.nio.channels.Channels
  import scala.xml.Node
  import scala.util.control.Exception.ultimately
  import scala.xml.Utility
  import scala.xml.MinimizeMode

  val fileName = "D:/Workspaces/Eclipse/baiduapp/tmp"

  final def save(filename: String, node: Node, enc: String = "UTF-8", xmlDecl: Boolean = false): Unit = {
    val fos = new FileOutputStream(filename)
    val w = Channels.newWriter(fos.getChannel(), enc)

    ultimately(w.close())(
      write(w, node, enc, xmlDecl))
  }

  final def write(w: java.io.Writer, node: Node, enc: String, xmlDecl: Boolean, /* doctype: dtd.DocType, */ minimizeTags: MinimizeMode.Value = MinimizeMode.Default) {
    /* TODO: optimize by giving writer parameter to toXML*/
    if (xmlDecl) w.write("<?xml version='1.0' encoding='" + enc + "'?>\n")
    /* if (doctype ne null) w.write( doctype.toString() + "\n")*/
    w.write(Utility.serialize(node, minimizeTags = minimizeTags).toString)
  }

  final def close(w: java.io.Writer): Unit = {
    //TODO :״̬����
    w.flush()
    w.close()
  }

  def watch(status: List[Boolean]) {
    //TODO ״̬���
  }

  import scala.concurrent.duration._
  def receive = {
    case  msg @_ =>
      ultimately(this.context.system.shutdown){log.info(msg.toString)}

  }
}

